package edu.cth.scrabbit.client.view.panels;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import edu.cth.scrabbit.client.ctrl.ClientController;
import edu.cth.scrabbit.shared.GameLetter;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class LetterTile extends javax.swing.JPanel {
	private JButton letterButton;

	GameLetter letter;

	private int yIndex = -1;
	private int xIndex = -1;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */

	public LetterTile() {
		super();
		initGUI();
	}

	public void setIndex(int xIndex, int yIndex) {
		this.yIndex = yIndex;
		this.xIndex = xIndex;
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent) this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(28, 28));
			this.setSize(28, 28);
			{
				letterButton = new JButton();
				letterButton.setName("letterButton");
				letterButton.setFont(new Font("Arial", Font.PLAIN, 9));
				letterButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
						0));
				letterButton.setAction(getAppActionMap().get("selectAction"));
				letterButton.setAction(getAppActionMap().get("selectAction"));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addComponent(letterButton, GroupLayout.PREFERRED_SIZE, 28,
							GroupLayout.PREFERRED_SIZE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addComponent(letterButton, 0, 28, Short.MAX_VALUE));
			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setLabel(GameLetter letter) {
		this.letter = letter;
		if (letter.isDummy()) {
			letterButton.setEnabled(true);
			letterButton.setText("");
		} else {
			letterButton.setText(letter.toString());
			letterButton.setEnabled(false);
			letterButton.setForeground(Color.RED);
		}
	}

	/**
	 * Returns the action map used by this application. Actions defined using
	 * the Action annotation are returned by this method
	 */
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}

	@Action
	public void selectAction() {
		ClientController.getInstance().selectGameBoardTile(this.letter);
	}

	public int getYIndex() {
		return yIndex;
	}

	public int getXIndex() {
		return xIndex;
	}

	public GameLetter getLetter() {
		if (letter != null) {
			letter.setPosition(xIndex, yIndex);
		}
		return letter;
	}

	public void setButtonEnabled(boolean b) {
		letterButton.setEnabled(b);
	}
}
